<?php

class allocationsmodel extends CI_Model
{

    public function __construct()
    {
        $this->load->database();
    }

    public function view()
    {
        $this->load->helper('url');
        $pdate = $this->input->post('allocation_date');
        $region = $this->input->post('region');
        $location = $this->input->post('location');
        if (($region == '') && ($location == '') && ($pdate == '')) {
            $query = $this->db->get('allocations');
        } else if (($region == 'Select region') && ($location == 'Select location') && ($pdate == '')) {
            $query = $this->db->get('allocations');
        } else if (!(($region == 'Select region') || ($location == 'Select location') || ($pdate == ''))) {
            $this->db->where('region', $region);
            $this->db->where('location', $location);
            $this->db->where('allocation_date', $pdate);
            $query = $this->db->get('allocations');
        } elseif (($location == 'Select location') && ($pdate == '')) {
            $this->db->where('region', $region);
            $query = $this->db->get('allocations');
        } elseif (($region == 'Select region') && ($pdate == '')) {
            $this->db->where('location', $location);
            $query = $this->db->get('allocations');
        } elseif (($region == 'Select region') && ($location == 'Select location')) {
            $this->db->where('allocation_date', $pdate);
            $query = $this->db->get('allocations');
        } elseif (($region =='Select region')) {
            $this->db->where('location', $location);
            $this->db->where('allocation_date', $pdate);
            $query = $this->db->get('allocations');

        } elseif (($location == 'Select location')) {
            $this->db->where('region', $region);
            $this->db->where('allocation_date', $pdate);
            $query = $this->db->get('allocations');

        } elseif (($pdate == '')) {
            $this->db->where('region', $region);
            $this->db->where('location', $location);
            $query = $this->db->get('allocations');

        }
        $data['count'] = $query->num_rows();
        $data['allocations'] = $query->result_array();
        return $data;

    }

    public function viewind()
    {
        $this->load->helper('url');
        $id = $this->input->post('d_id');
        $query = $this->db->get_where('allocations', array('id' => $id));
        return $query->row_array();
    }

    public function addallocation()
    {
        $this->load->helper('url');
        $data = array(
            'id' => $this->input->post('id'),
            'serial_id' => $this->input->post('serial_id'),
            'employee_id' => $this->input->post('employee_id'),
            'allocation_date' => $this->input->post('allocation_date'),
            'allocation_status' => $this->input->post('allocation_status'),
            'region' => $this->input->post('region'),
            'location' => $this->input->post('location'),
        );
        return $this->db->insert('allocations', $data);
    }

    public function getData()
    {
        $this->load->helper('url');
        $id = $this->input->post('id');
        $query = $this->db->get_where('allocations', array('id' => $id));
        return $query->row_array();

    }

    public function updateData()
    {
        $this->load->helper('url');
        $data = array(
            'id' => $this->input->post('device_id'),
            'serial_id' => $this->input->post('serial_id'),
            'employee_id' => $this->input->post('employee_id'),
            'allocation_date' => $this->input->post('allocation_date'),
            'allocation_status' => $this->input->post('allocation_status'),
            'region' => $this->input->post('region'),
            'location' => $this->input->post('location'),
        );
        $this->db->where('id', $data['id']);
        return $this->db->update('allocations', $data);
    }
}